//
//  NITitleView.m
//  HavalConnect
//
//  Created by NAVINFO on 2017/10/23.
//  Copyright © 2017年 mengy. All rights reserved.
//

#import "NITitleView.h"
//#import "NIAvatarView.h"
@interface NITitleView()
{
    
    IBOutlet UIButton *backButton;
    IBOutlet UIButton *rightButton;
    IBOutlet UIButton *leftButton;
    IBOutlet UILabel *titleLabel;
    IBOutlet UIView *contentView;
    __weak IBOutlet NSLayoutConstraint *backBtnAspectLayoutConstraint;
    __weak IBOutlet NSLayoutConstraint *leftButtonWidthLY;
    
    __weak IBOutlet UIView *titleContentView;
    __weak IBOutlet NSLayoutConstraint *titleButtonImageWidthLY;
    __weak IBOutlet UIImageView *pullDownImageView;
    
    BOOL pullDown;
    UITapGestureRecognizer *singleTap;
//    __weak IBOutlet NIAvatarView *AvatarView;
    __weak IBOutlet UIView *lineView;
    
}

@end

@implementation NITitleView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self awakeFromNib];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self awakeFromNib];
    }
    return self;
}


-(void)awakeFromNib
{
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    
    [self addSubview:contentView];
    
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
    titleLabel.hidden = YES;
    backButton.hidden = YES;
    leftButton.hidden = YES;
//    AvatarView.hidden = YES;
//    [AvatarView hiddenPen:YES];
//    [AvatarView hiddenPoint:YES];
//    AvatarView.userInteractionEnabled = YES;
//    AvatarView.backgroundColor = [UIColor clearColor];
    leftButtonWidthLY.constant = 0;
    rightButton.hidden = YES;
    self.backgroundColor = [UIColor clearColor];
    contentView.backgroundColor = [UIColor clearColor];
    titleButtonImageWidthLY.constant = 0;
    
    if (singleTap == nil) {
        singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickPullDown)];
    }
    [titleContentView addGestureRecognizer:singleTap];
    lineView.hidden = YES;
    [titleLabel setTextColor:[UIColor whiteColor]];
    
}

-(void)showPulldown:(BOOL)showPullDown 
{
    if (showPullDown) {
        titleButtonImageWidthLY.constant = 15;
        pullDown = NO;
    }
    else
    {
        titleButtonImageWidthLY.constant = 0;
    }
}

-(void)setTitle:(NSString *)title
{
    titleLabel.text = title;
    titleLabel.hidden = NO;
    [titleLabel setTextColor:[UIColor blackColor]];
    if (_isRemoteView) {
        lineView.hidden = NO;
        [titleLabel setTextColor:[UIColor blackColor]];
    }
}

- (NSString *)getTitle{
    return titleLabel.text;
}

-(void)setAvatarViewWithImage:(UIImage *)image action:(SEL)action target:(id)target showShadow:(BOOL)showShadow
{
    /**
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
    [AvatarView addGestureRecognizer:tap];
    [AvatarView setAvatarImage:image];
    if (!showShadow) {
        [AvatarView cutFlag:NO];
    }
    else
    {
        [AvatarView cutFlag:YES];
        
    }
    AvatarView.hidden = NO;
     */
}

-(void)setBackBtnWithImageName:(NSString *)imageName
{
    [backButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [backButton setTitle:@"" forState:UIControlStateNormal];
    backButton.hidden = NO;
}

-(void)setBackBtnWithImageName:(NSString *)imageName action:(SEL)action target:(id)target
{
    [backButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [backButton setTitle:@"" forState:UIControlStateNormal];
    [backButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    backButton.hidden = NO;
}

-(void)setRightBtn2WithImageName:(NSString *)imageName action:(SEL)action target:(id)target
{
    
    [leftButton setTitle:@"" forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    leftButton.hidden = NO;
    leftButtonWidthLY.constant = 30;
}

-(void)setRightBtn1WithImageName:(NSString *)imageName action:(SEL)action target:(id)target
{
    [rightButton setTitle:@"" forState:UIControlStateNormal];
    
    [rightButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [rightButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    rightButton.hidden = NO;
}


-(void)setRightBtn1WithTitle:(NSString *)title backgroundImageName:(NSString *)imageName action:(SEL)action target:(id)target
{
    
    if (imageName) {
        [rightButton setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    else
    {
        [rightButton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    }
    if (title) {
        [rightButton setTitle:title forState:UIControlStateNormal];
    }
    else
    {
        
        [rightButton setTitle:@"" forState:UIControlStateNormal];
    }
    [rightButton setImage:nil forState:UIControlStateNormal];
    [rightButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    rightButton.titleLabel.lineBreakMode =  NSLineBreakByTruncatingTail;
    rightButton.hidden = NO;
}

-(void)setRightBtn2WithTitle:(NSString *)title backgroundImageName:(NSString *)imageName action:(SEL)action target:(id)target
{
    if (imageName) {
        [leftButton setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    else
    {
        [leftButton setBackgroundImage:nil forState:UIControlStateNormal];
    }
    if (title) {
        [leftButton setTitle:title forState:UIControlStateNormal];
    }
    else
    {
        
        [leftButton setTitle:@"" forState:UIControlStateNormal];
    }
    [leftButton setImage:nil forState:UIControlStateNormal];
    [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    leftButton.hidden = NO;
    leftButtonWidthLY.constant = 30;
}

-(void)setBackBtnHidden:(BOOL)hidden
{
    backButton.hidden = hidden;
}

-(void)hiddenRightBtn2
{
    
    leftButton.hidden = YES;
    leftButtonWidthLY.constant = 0;
    [leftButton removeTarget:nil
                       action:NULL
             forControlEvents:UIControlEventAllEvents];
}

-(void)hiddenRightBtn1
{
    
    rightButton.hidden = YES;
    [rightButton removeTarget:nil
                       action:NULL
             forControlEvents:UIControlEventAllEvents];
}

-(void)setRightBtn2Enable:(BOOL)enable
{
    leftButton.enabled = enable;
}

-(void)setRightBtn1Enable:(BOOL)enable
{
    
    rightButton.enabled = enable;
}

-(void)onClickPullDown
{
    pullDown = !pullDown;
    if (self.delegate && [self.delegate respondsToSelector:@selector(onClickPulldownButton:)]) {
        [self.delegate onClickPulldownButton:pullDown];
    }
    if (pullDown) {
        pullDownImageView.image = [UIImage imageNamed:@"account_share_ic_up"];
    }
    else
    {
        pullDownImageView.image = [UIImage imageNamed:@"account_share_ic_down"];
    }
}


-(void)setPullDown:(BOOL)flag
{
    pullDown = flag;
    if (pullDown) {
        pullDownImageView.image = [UIImage imageNamed:@"account_share_ic_up"];
    }
    else
    {
        pullDownImageView.image = [UIImage imageNamed:@"account_share_ic_down"];
    }
}

-(BOOL)avatarViewHidden
{
//    return AvatarView.hidden;
    return YES;
}


@end
